package com.springboot.prac.prac02_draw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

/**
 * 画图主窗体
 */
public class DrawPictureFrame extends JFrame {

    // 创建一个8位BGR颜色分量的图像
    BufferedImage bufferedImage = new BufferedImage(570, 390, BufferedImage.TYPE_INT_BGR);
    // 获取图像的绘图对象
    Graphics gs = bufferedImage.getGraphics();
    // 将绘图对象转换成Graphics2D类型
    Graphics2D g = (Graphics2D) gs;
    // 创建画布对象
    DrawPictureCanvas canvas = new DrawPictureCanvas();
    // 定义前景色
    Color foreColor = Color.black;
    // 定义背景色
    Color backgroundColor = Color.white;
    int x = -1;
    int y = -1;
    boolean rubber = false;
    // 工具栏
    private JToolBar toolBar;
    // 橡皮按钮
    private JButton eraserButton;
    // 细线按钮
    private JToggleButton strokeButton1;
    // 粗线按钮
    private JToggleButton strokeButton2;
    // 较粗按钮
    private JToggleButton strokeButton3;
    // 背景色按钮
    private JButton backgroundButton;
    // 前景色按钮
    private JButton foregroundButton;
    // 清除按钮
    private JButton clearButton;
    // 保存按钮
    private JButton saveButton;
    // 图形按钮
    private JButton shapeButton;

    public DrawPictureFrame() {
        // 窗体不能改变大小
        setResizable(false);
        // 设置标题
        setTitle("我的画图工具");
        // 窗体关闭则停止程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口位置和宽高
        setBounds(500, 100, 574, 460);
        // 组件初始化
        init();
        // 添加监听组件
        addListener();
    }

    private void init() {
        // 用背景色设置绘图对象的颜色
        g.setColor(backgroundColor);
        // 用背景色填充整个画布
        g.fillRect(0, 0, 570, 390);
        // 用前景色设置绘图对象的颜色
        g.setColor(foreColor);
        // 设置画布的图像
        canvas.setImage(bufferedImage);
        // 将画布添加到窗体容器默认布局中的中部位置
        getContentPane().add(canvas);

        toolBar = new JToolBar();
        getContentPane().add(toolBar, BorderLayout.NORTH);

        saveButton = new JButton("保存");
        toolBar.add(saveButton);
        toolBar.addSeparator();

        strokeButton1 = new JToggleButton("细线");
        strokeButton1.setSelected(true);
        toolBar.add(strokeButton1);

        strokeButton2 = new JToggleButton("粗线");
        toolBar.add(strokeButton2);

        strokeButton3 = new JToggleButton("较粗");
        toolBar.add(strokeButton3);

        ButtonGroup strokeGroup = new ButtonGroup();
        strokeGroup.add(strokeButton1);
        strokeGroup.add(strokeButton2);
        strokeGroup.add(strokeButton3);
        toolBar.add(strokeButton3);
        toolBar.addSeparator();

        backgroundButton = new JButton("背景颜色");
        toolBar.add(backgroundButton);

        foregroundButton = new JButton("前景颜色");
        toolBar.add(foregroundButton);

        clearButton = new JButton("清除");
        toolBar.add(clearButton);

        eraserButton = new JButton("橡皮");
        toolBar.add(eraserButton);

    }

    private void addListener() {
        canvas.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(final MouseEvent e) {
                if (x > 0 && y > 0) {
                    if (rubber) {
                        g.setColor(backgroundColor);
                        g.fillRect(x, y, 10, 10);
                    } else {
                        g.drawLine(x, y, e.getX(), e.getY());
                    }
                }
                x = e.getX();
                y = e.getY();
                canvas.repaint();
            }
        });

        canvas.addMouseListener(new MouseAdapter() {
            public void mouseReleased(final MouseEvent e) {
                x = -1;
                y = -1;
            }
        });

        // 为组件添加动作监听 细线按钮、粗线按钮、较粗按钮
        strokeButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BasicStroke basicStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
                g.setStroke(basicStroke);
            }
        });

        strokeButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BasicStroke basicStroke = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
                g.setStroke(basicStroke);
            }
        });

        strokeButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BasicStroke basicStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
                g.setStroke(basicStroke);
            }
        });

        // 背景颜色按钮、前景颜色按钮动作监听
        backgroundButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN);
                if (color != null) {
                    backgroundColor = color;
                }

                backgroundButton.setBackground(backgroundColor);
                g.setColor(backgroundColor);
                g.fillRect(0, 0, 570, 390);
                g.setColor(foreColor);
                canvas.repaint();
            }
        });

        foregroundButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN);
                if (color != null) {
                    foreColor = color;
                }

                backgroundButton.setForeground(foreColor);
                g.setColor(foreColor);
            }
        });

        // 清除按钮监听事件
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                g.setColor(backgroundColor);
                g.fillRect(0, 0, 570, 390);
                g.setColor(foreColor);
                canvas.repaint();
            }
        });

        // 橡皮按钮监听事件
        eraserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (eraserButton.getText().equals("橡皮")) {
                    rubber = true;
                    eraserButton.setText("画图");
                } else {
                    rubber = false;
                    eraserButton.setText("橡皮");
                    g.setColor(foreColor);
                }
            }
        });
    }

    public static void main(String[] args) {
        // 创建窗体对象
        DrawPictureFrame drawPictureFrame = new DrawPictureFrame();
        // 设置窗体可见
        drawPictureFrame.setVisible(true);
    }

}